//
//  AcceptNewFriendBoard.m
//  iHeima
//
//  Created by WL-DZ-PGDN-007 on 14-6-30.
//  Copyright (c) 2014年 leon. All rights reserved.
//

#import "AcceptNewFriendBoard.h"

@interface AcceptNewFriendBoard ()<UIWebViewDelegate>{
    UIWebView* _webview;
    
    
    NSString* _action;
    NSString* _id;
}

@end

@implementation AcceptNewFriendBoard

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


DEF_SIGNAL(WECHAT)
DEF_SIGNAL(WECHAT_CIRCLE)
DEF_SIGNAL(SMS)

- (void)load
{
}

- (void)unload
{
}

#pragma mark - Signal

ON_CREATE_VIEWS( signal )
{
    self.title = @"新的朋友";
    [self showBarButton:BeeUINavigationBar.LEFT image:IMAGESTRING(@"navi_back")];
    [self showNaviBar];
    [self loadContent];
}

ON_DELETE_VIEWS( signal )
{
}

ON_LAYOUT_VIEWS( signal )
{
}

ON_WILL_APPEAR( signal )
{
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

ON_DID_APPEAR( signal )
{
}

ON_WILL_DISAPPEAR( signal )
{
}

ON_DID_DISAPPEAR( signal )
{
}

ON_SIGNAL3( BeeUINavigationBar, LEFT_TOUCHED, signal )
{
    [self.stack popBoardAnimated:YES];
}

ON_SIGNAL3( BeeUINavigationBar, RIGHT_TOUCHED, signal )
{
}




- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}


#pragma mark - Signal


- (void)loadContent{
    _webview = [[UIWebView alloc]init];
    _webview.frame = CGRectMake(0, 0,self.viewWidth, self.viewHeight-(IOS7_OR_LATER?64:44));
    [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/app/friendslist?uid=%@",kBaseUrl,kUserInfo[@"uid"]]]]];
    
    NSLog(@"uid:%@",kUserInfo[@"uid"]);
    
    
    //    [_webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/app/friendslist?uid=1",kBaseUrl]]]];
    
    _webview.delegate = self;
    
    [self.view addSubview:_webview];

}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    
    
    
    NSString* nextUrl = [request.URL.absoluteString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    if([nextUrl componentsSeparatedByString:@"#"].count == 5){
        
        NSLog(@"------整个:%@",nextUrl);
        
        
        
        [webView stopLoading];
        
        NSArray* ac = [nextUrl componentsSeparatedByString:@"#"];
        NSArray* arr = [[ac objectAtIndex:2] componentsSeparatedByString:@"&"];
        NSLog(@"----%@",[arr objectAtIndex:0]);
        NSLog(@"=====%@",[arr objectAtIndex:1]);
        
        //action
        _action =[[[arr objectAtIndex:0] componentsSeparatedByString:@"="] objectAtIndex:1];
        NSLog(@"动作：%@",[[[arr objectAtIndex:0] componentsSeparatedByString:@"="] objectAtIndex:1]);
        _id = [[[arr objectAtIndex:1] componentsSeparatedByString:@"="] objectAtIndex:1];
        NSLog(@"fuid:%@",[[[arr objectAtIndex:1] componentsSeparatedByString:@"="] objectAtIndex:1]);
        
        if ([_action isEqualToString:@"agree"] && _id) {
            //发送 接受好友请求
            
            
            [self addFriendByFid:_id];
            
        }
        
    }
    
    
    return YES;
}
//接受好友请求, UID与FUID相反
- (void)addFriendByFid:(NSString*)fuid{
    

    [self POST:[NSString stringWithFormat:@"%@/apiuser/addfriend",kBaseUrl]].PARAM(@"uid",kUserInfo[@"uid"]).PARAM(@"fuid",fuid).PARAM(@"message",@"addFriend");
    
    
    
    
    
}

- (void)handleRequest:(BeeRequest *)request
{
    if(request.failed)
    {
        [[BeeUITipsCenter sharedInstance] dismissTips];
        NETWORK_ERROR
    }
    else if (request.succeed)
    {
        id json = [request.responseString mutableObjectFromJSONString];
        NSLog(@"接受好友请求:%@",json);
        
        
        
        NSLog(@"发送成功");
        
        UIAlertView* av = [[UIAlertView alloc]initWithTitle:@"已接受对方的好友邀请" message:@"" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:Nil, nil];
        [av show];
        
        
    }
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
